package com.atguigu.web;

import com.atguigu.pojo.Book;
import com.atguigu.pojo.Page;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class BookServlet extends BaseServlet{
    private BookService bookService = new BookServiceImpl();

    /**
     * 处理分页功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        int pageNo ,pageSize;
        //1.获取请求的参数
        if(request.getParameter("pageNo") == null){
            pageNo = 1;
        }else{
            pageNo = Integer.valueOf(request.getParameter("pageNo"));
        }

        if(request.getParameter("pageSize") == null){
            pageSize = Page.PAGE_SIZE;
        }else{
            pageSize = Integer.valueOf(request.getParameter("pageSize"));
        }
        //2.调用BookService.page方法:Page对象
        Page<Book> page = bookService.page(pageNo,pageSize);

        page.setUrl("manager/bookServlet?action=page");

        //3.保存page对象到request域中
        request.setAttribute("page",page);
        //4.请求转发到pages/manager/book_manager.jsp域中
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request,response);
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("text/html;charset=utf-8");
        //请求解决乱码
        request.setCharacterEncoding("utf-8");
        //响应解决乱码
        response.setCharacterEncoding("utf-8");

        //1.获取请求对象的参数-->封装成为Book对象
        Book book = WebUtils.copyParamToBean(request.getParameterMap(),new Book());

        int pageNo = Integer.valueOf(request.getParameter("pageNo"))+1;
        //2.调用BookServlet.add(Book)保存图书
        bookService.addBook(book);
        //3.跳到图书页面
        response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=page&pageNo="+pageNo);
    }

    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //1.获取需要删除的图书id
        String id = request.getParameter("id");
        //2.调用bookService.deleteById()方法删除图书
        bookService.deleteBookById(Integer.valueOf(id));
        //3.重定向回图书列表管理页面
        response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=page&pageNo="+request.getParameter("pageNo"));
    }

    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //1.获取请求的参数,封装成为Book对象
        Book book = WebUtils.copyParamToBean(request.getParameterMap(),new Book());
        //2.调用bookService.updateBook(book);修改图书
        bookService.updateBook(book);
        //3.重定向回图书列表管理页面
        response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=page&pageNo="+request.getParameter("pageNo"));
    }

    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //1.通过BookServlet查询全部图书
        List<Book> books = bookService.queryBooks();
        //2.把全部图书保存到Request域中
        request.setAttribute("books",books);
        //3.请求转发到/pages/manager/book_manager.jsp页面
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request,response);
    }

    protected void getBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //1.获取请求的图书编号
        Integer id = Integer.valueOf(request.getParameter("id"));
        //2.调用bookService.queryBookById查询图书
        Book book = bookService.queryBookById(id);
        //3.保存图书到Request域中
        request.setAttribute("book",book);
        //4.请求转发到 pages/manager/book_edit.jsp域中
        request.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(request,response);
    }

}
